<?php
/**
 * @orm tablename wb_pages
 */
class WbPage extends DomArObject {

	/**
	 * @orm owns children WbPage inverse parent
	 */
	public $pages;
	/**
	 * @orm has parent WbPage inverse pages
	 * @orm listener afterSet afterSetParent
	 */
	public $parent;
	/**
	 * @orm has parent WbPage inverse allPages
	 */
	public $site;
	/**
	 * @orm char(50)
	 */
	public $template;
	/**
	 * @orm has many WbPage inverse site
	 */
	public $allPages;
	/**
	 * @orm char(255)
	 */
	public $name;
	/**
	 * @orm plugin SimpleOrder({parent},10)
	 */
	public $order;
	/**
	 * @orm boolean
	 */
	public $active = true;
	/**
	 * @orm char(40) unique
	 */
	public $urlKey;
	/**
	 * @orm char(255)
	 */
	public $metaTitle;
	/**
	 * @orm char(255)
	 */
	public $metaDescription;
	/**
	 * @orm char(255)
	 */
	public $metaKeywords;
	/**
	 * @orm boolean
	 * @orm listener beforeSave beforeSaveIsFrontPage
	 */
	public $isFrontPage = false;
	
	/**
	 * @return WbSite
	 */
	public function getRoot() {
		return $this->site;
	}
	/**
	 * Returns root page (Root page is not WbSite, but a child of it.)
	 * @return unknown_type
	 */
	public function getRootPage() {
		if( $this->parent instanceof WbSite ) {
			return $this;
		} else {
			return $this->parent->getRootPage();
		}
	}
	
	public function getTemplates() {
		$rootPath = DOM_TEMPLATES_DIR .'Wb/'.get_class($this).'/';
		$list = array();
		foreach( glob( $rootPath.'*') as $path) {
			$template = str_replace( $rootPath, '', $path);
			$list[$template] = $template;
		}
		return $list;
	}

	public function getTemplatePath( $add = '' ) {
		return '/Wb/'.get_class($this).'/'.$this->template.'/'.$add;
	}
	
	public function getPageUrlKey() {
		return $this->urlKey ? $this->urlKey : $this->id;
	}
	
	public function getChildPages( $activeOnly = false ) {
		$query = $this->pages->query()->orderBy('t.order');
		if( $activeOnly ) {
			$query->where('t.active=1');
		} 
		return $query->commit()->toArray();
	}
	
	public function getChildrenTree( $activeOnly = false, &$tree = array() ) {
		foreach( $this->getChildPages($activeOnly) as $page ) {
			$tree[] = $page;
			$page->getChildrenTree( $activeOnly, $tree );
		}
		return $tree;
	}
	
	public function getTree( $activeOnly = false, &$tree = array() ) {
		$tree[] = $this;
		foreach( $this->getChildPages($activeOnly) as $page ) {
			$page->getTree( $activeOnly, $tree );
		}
		return $tree;
	}
	
	public function isParentOf( WbPage $page ) {
		if( $this->parent == NULL ) {
			return false;
		}
		
		if( $this->parent->id == $page->id ) {
			return true;
		}
		return $this->parent->isParentOf( $page );
	}
	
	public function getChildPagesArray( $activeOnly = false ) {
		global $SITE;
		$list = array();
		foreach( $this->getChildPages( $activeOnly ) as $page ) {
			$list[] = array(
				'id' => $page->id,
				'name' => $page->name,
				'level' => 0,
				'urlKey' => $page->getPageUrlKey(),
				'selected' => $SITE instanceof WbWebSite && $SITE->isSelected( $page->id),
			);
		}
		return $list;
	}
		
	public function getChildrenTreeArray( $activeOnly = false, &$tree = array(), $level = 0 ) {
		global $SITE;
		foreach( $this->getChildPages( $activeOnly ) as $page ) {
			$tree[] = array(
				'id' => $page->id, 
				'name' => $page->name, 
				'level' => $level,
				'urlKey' => $page->getPageUrlKey(),
				'selected' => $SITE instanceof WbWebSite && $SITE->isSelected( $page->id),
			);
			$page->getChildrenTreeArray( $activeOnly, $tree, $level+1 );
		}
		return $tree;
	}
	
	public function getTreeArray( $activeOnly = false, &$tree = array(), $level = 0 ) {
		global $SITE;
		$tree[] = array(
			'id' => $this->id, 
			'name' => $this->name, 
			'level' => $level,
			'urlKey' => $this->getPageUrlKey(), 
			'selected' => $SITE instanceof WbWebSite && $SITE->isSelected( $page->id),
		);
		foreach( $this->getChildPages( $activeOnly ) as $page ) {
			$page->getTreeArray( $activeOnly, $tree, $level+1 );
		}
		return $tree;
	}

	public function beforeSaveIsFrontPage() {
		if( $this->isFrontPage == true ) {
			DomSql::query('UPDATE '.WbPage::getDbTable().' SET isFrontPage=0 WHERE site ='.$this->getRoot()->id.'');
		}
	}
	
	public function afterSetParent() {
		if( $this->parent ) {
			if( $this->parent instanceof WbSite ) {
				$this->site = $this->parent;
			} else {
				$this->site = $this->parent->site;
			}
		} else {
			$this->site = NULL;
		}
	}

	public function getTreeIcon() {
		return 'module';
	}
}